<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <h1 id="logo">
          <a href="#" title="谷粒学院">
            <img src="~/assets/img/logo.png" width="100%" alt="谷粒学院">
          </a>
        </h1>
        <div class="h-r-nsl">
          <ul class="nav">
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>师资</a>
            </router-link>
            <router-link to="/article" tag="li" active-class="current">
              <!-- <a>文章</a> -->
              <a>手记</a>
            </router-link>
            <router-link to="/forum" tag="li" active-class="current">
              <!-- <a>问答</a> -->
              <a>论坛</a>
            </router-link>
          </ul>
          <!-- / nav -->
          <ul class="h-r-login">
              <li v-if="!loginInfo.id" id="no-login">
                <a href="/login" title="登录">
                  <em class="icon18 login-icon">&nbsp;</em>
                  <span class="vam ml5">登录</span>
                </a>
                <a href="/register" title="注册">
                  <span class="vam ml5">注册</span>
                </a>
              </li>
              <li v-if="loginInfo.id" id="is-login-one" class="mr10">
                <a id="headerMsgCountId" href="#" title="消息">
                  <em class="icon18 news-icon">&nbsp;</em>
                </a>
                <q class="red-point" style="display: none">&nbsp;</q>
              </li>
              <li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
                <el-dropdown @command="handleCommand">
                  <a href="/ucenter" title>
                    <img
                      :src="loginInfo.avatar"
                      width="30"
                      height="30"
                      class="vam picImg"
                      alt
                    >
                    <span id="userName" class="vam disIb">{{ loginInfo.nickname }}</span>
                  </a>
                  <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="a">个人中心</el-dropdown-item>
                      <el-dropdown-item command="b">我的手记</el-dropdown-item>
                      <el-dropdown-item command="c">螺蛳粉</el-dropdown-item>
                      <el-dropdown-item command="d" disabled>双皮奶</el-dropdown-item>
                      <el-dropdown-item command="e" divided>蚵仔煎</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <a href="javascript:void(0);" title="退出" @click="logout()" class="ml5">退
              出</a>
              </li>
              <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>
          <aside class="h-r-search">
            <form action="#" method="post">
              <label class="h-r-s-box">
                <input type="text" placeholder="输入你想学的课程" name="queryCourse.courseName" value>
                <button type="submit" class="s-btn">
                  <em class="icon18">&nbsp;</em>
                </button>
              </label>
            </form>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>
    <!-- /公共头引入 -->
    <!-- 手记 公共头引入 -->
    <div class="m-content">
        <div class="logoBlock">
            <!-- <el-avatar :size="50"></el-avatar>
            <div>用户名</div> -->
            <a href="#" title="手记">
                <img src="~/assets/img/articleHeader-logo.png" width="100%" height="100%" alt="手记">
            </a>
        </div>
        <div class="searchArticle">
            <el-input placeholder="搜索感兴趣的知识和文章" class="input-with-select">
                <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input>
        </div>
        <div class="writeArticle">
            <el-button type="primary" icon="el-icon-edit">写文章</el-button>
        </div>
    </div>

    <nuxt/>
    
    <!-- 公共底引入 -->
    <footer id="footer">
      <section class="container">
        <div class>
          <h4 class="hLh30">
            <span class="fsize18 f-fM c-999">友情链接</span>
          </h4>
          <ul class="of flink-list">
            <li>
              <a href="http://www.atguigu.com/" title="尚硅谷" target="_blank">尚硅谷</a>
            </li>
          </ul>
          <div class="clear"></div>
        </div>
        <div class="b-foot">
          <section class="fl col-7">
            <section class="mr20">
              <section class="b-f-link">
                <a href="#" title="关于我们" target="_blank">关于我们</a>|
                <a href="#" title="联系我们" target="_blank">联系我们</a>|
                <a href="#" title="帮助中心" target="_blank">帮助中心</a>|
                <a href="#" title="资源下载" target="_blank">资源下载</a>|
                <span>服务热线：010-56253825(北京) 0755-85293825(深圳)</span>
                <span>Email：info@atguigu.com</span>
              </section>
              <section class="b-f-link mt10">
                <span>©2018课程版权均归谷粒学院所有 京ICP备17055252号</span>
              </section>
            </section>
          </section>
          <aside class="fl col-3 tac mt15">
            <section class="gf-tx">
              <span>
                <img src="~/assets/img/wx-icon.png" alt>
              </span>
            </section>
            <section class="gf-tx">
              <span>
                <img src="~/assets/img/wb-icon.png" alt>
              </span>
            </section>
          </aside>
          <div class="clear"></div>
        </div>
      </section>
    </footer>
    <!-- /公共底引入 -->

  </div>
</template>
<script>
import '~/assets/css/reset.css'
import '~/assets/css/theme.css'
import '~/assets/css/global.css'
import '~/assets/css/web.css'
import '~/assets/css/base.css'
import '~/assets/css/activity_tab.css'
import '~/assets/css/bottom_rec.css'
import '~/assets/css/nice_select.css'
import '~/assets/css/order.css'
import '~/assets/css/swiper-3.3.1.min.css'
import "~/assets/css/pages-weixinpay.css"

import loginApi from '@/api/login'
import cookie from 'js-cookie'

export default {
  data() {
    return {
      token:'',
      loginInfo: {
        id: '',
        age: '',
        avatar: '',
        mobile: '',
        nickname: '',
        sex: ''
      }
    }
  },
  created() {
    //判断路径是否包含token参数
    this.token = this.$route.query.token
    // console.log('判断路径是否包含token参数')
    // console.log(this.token)
    if (this.token) {
      this.wxLoging()
    } else {
      this.getInfo()
    }
  },
  methods: {

    //用户头像位置下拉菜单点击事件
    handleCommand(command) {
         if (command === 'a') {
            this.$message('click on item ' + command);
         }
    },

    wxLoging() {
      //将token放到cookie里，让请求头携带token发送请求
      cookie.set("token", this.token, {domain:'localhost'})
      //利用token得到用户信息
      loginApi.getMemberInfo()
            .then(response => {
              this.loginInfo = response.data.data.userInfo
              //把用户信息保存到cookie里
              //注意这里一定要将JSON对象转为字符串再存储！！！
              cookie.set("ucenter", JSON.stringify(this.loginInfo), {domain:'localhost'})
            })
    },
    getInfo() {
      //从cookie获取用户信息
      var userStr = cookie.get('ucenter')
      // 把字符串转换json对象(js对象)
      if(userStr) {
        this.loginInfo = JSON.parse(userStr)
      }
    },
    logout() {
      //清空cookie信息
      cookie.set("token", '', {domain:'localhost'})
      cookie.set("ucenter", '', {domain:'localhost'})
      //跳回首页，类似页面刷新
      window.location.href="/"
    }
  },
};
</script>

<style scoped>
  .m-content {
    max-width: 1150px;
    margin: 0 auto;
    align-items: center;
    display: flex;
    position: relative;
  }
  .logoBlock {
    width: 10%;
    padding: 5px;
    display: inline-block;
  }
  .searchArticle {
    background-color: yellow;
    vertical-align: top;
    display: inline-block;
    position: absolute;
    right: 130px;
  }
  .el-input {
    /* 搜索框长度 */
    width: 400px;
  }
  .writeArticle {
    background-color: cadetblue;
    /* https://blog.csdn.net/qq_44131760/article/details/104755826  解释了为什么使用 vertical-align*/
    vertical-align: top;
    display: inline-block;
    position: absolute;
    right: 10px;
  }
</style>



